package com.atguigu.jxc.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.atguigu.jxc.dao.PurchaseListDao;
import com.atguigu.jxc.dao.PurchaseListGoodsDao;
import com.atguigu.jxc.dao.UserDao;
import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.PurchaseList;
import com.atguigu.jxc.entity.PurchaseListGoods;
import com.atguigu.jxc.entity.User;
import com.atguigu.jxc.service.GoodsService;
import com.atguigu.jxc.service.PurchaseListGoodsService;
import com.atguigu.jxc.service.PurchaseListService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 杨林
 * @create 2023-01-29 20:46 星期日
 * description:
 */
@Service
public class PurchaseListGoodsServiceImpl implements PurchaseListGoodsService {

    @Autowired
    PurchaseListService purchaseListService;

    @Autowired
    PurchaseListDao purchaseListDao;

    @Autowired
    PurchaseListGoodsDao purchaseListGoodsDao;

    @Autowired
    UserDao userDao;

    @Autowired
    GoodsService goodsService;
    /**
     * 进货单保存
     * @param purchaseList
     * @param purchaseListGoodsStr
     * @return
     */
    @Override
    public ServiceVO save(PurchaseList purchaseList, String purchaseListGoodsStr) {

        //将json字符串转换成实体类
        List<PurchaseListGoods> purchaseListGoodsList = JSONArray.parseArray(purchaseListGoodsStr,PurchaseListGoods.class);

        // 设置当前操作用户
        User currentUser = userDao.findUserByName((String) SecurityUtils.getSubject().getPrincipal());
        purchaseList.setUserId(currentUser.getUserId());

        //将purchaseList存入数据库
        purchaseListService.save(purchaseList);

        Integer purchaseListId = purchaseList.getPurchaseListId();

        for (PurchaseListGoods purchaseListGoods : purchaseListGoodsList) {
            purchaseListGoods.setPurchaseListId(purchaseListId);

            //保存进货单时需要修改库存
            Integer goodsNum = purchaseListGoods.getGoodsNum();
            String goodsCode = purchaseListGoods.getGoodsCode();
            boolean ifChange = goodsService.changeInventoryQuantity(goodsCode, goodsNum);

            if (ifChange == false){
                return new ServiceVO(ErrorCode.CHANGE_INVENTORY_QUANTITY_ERROR,ErrorCode.CHANGE_INVENTORY_QUANTITY_ERROR_MSG);
            }
        }

        //批量保存purchaseListGoods
        purchaseListGoodsDao.bachSave(purchaseListGoodsList);


        return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
    }

    /**
     * 进货单列表展示（可条件查询：单据号模糊、供应商、是否付款和日期区间）
     * @param purchaseNumber
     * @param supplierId
     * @param state
     * @param sTime
     * @param eTime
     * @return
     */
    @Override
    public Map<String, Object> list(String purchaseNumber,
                                    Integer supplierId,
                                    Integer state,
                                    String sTime,
                                    String eTime) {
        Map<String,Object> map = new HashMap<>();

        List<PurchaseList> purchaseLists =
                purchaseListDao.list(purchaseNumber,supplierId,state,sTime,eTime);
        map.put("rows",purchaseLists);
        return map;
    }

    /**
     * 进货单商品信息展示
     * @param purchaseListId
     * @return
     */
    @Override
    public Map<String, Object> goodsList(Integer purchaseListId) {
        Map<String,Object> map = new HashMap<>();

        //根据purchaseListId查询进货单商品信息
        List<PurchaseListGoods> purchaseListGoodsList =
                purchaseListGoodsDao.goodsList(purchaseListId);

        map.put("rows",purchaseListGoodsList);
        return map;
    }
}
